<?php

namespace App\Admin\Controllers;

use App\Models\Notice;
use App\Models\NoticeType;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Events\SendNoticeToAll;
use App\Admin\Extensions\SendNotice;




class NoticeController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('公告管理')
            ->description('公告管理')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增公告')
            ->description('新增公告')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Notice);

        $grid->model()->orderBy('id','desc');
        $grid->id('Id');
        $grid->user_id('发布人')->display(function($value){
            $admin = Admin::user()->where('id',1)->value('username');
            if($admin){
                return $admin;
            }
        });
        // $grid->type_id('类型')->display(function($value){
        //     $data = NoticeType::where('id',$value)->value('name');
        //     if($data){
        //         return $data;
        //     }
        // });
        $grid->title('标题');
        $grid->content('内容');
        // $grid->content('内容')->display(function($content){
        //     $content=htmlspecialchars_decode($content);
        //     if (mb_strlen($content,'utf-8') > 100){
        //         $content="<div style='overflow: hidden;text-overflow:ellipsis;white-space: nowrap;'>$content</div>";
        //     };
        //     return $content;
        // });
        $grid->status('状态')->display(function($value){
            return ($value == 0) ? "<span class='label label-warning'>未发布</span>" : "<span class='label label-success'>已发布</span>";
        });
        $grid->created_at('创建时间');

        $grid->actions(function($actions){
            $actions->disableView();
            // $actions->disableEdit();
            $actions->disableDelete();

            if($actions->row->status == 0){
                $actions->append(new SendNotice($actions->getKey()));
                // $actions->append("<a href='/admin/notice/sendNotice?id=" . $actions->getKey() . "'>" . "<button type='button' class='btn btn-success'>发布</button></a>");
            }else{
                $actions->disableEdit();
            }
        });
        return $grid;
    }
    public function sendNotice(Request $request)
    {
        
        $no_id = $request->id;
        $notice = Notice::where('id',$no_id)->first();
        $notice->status = 1;
        
        DB::beginTransaction();
        try {
            $notice->save();
            event(new SendNoticeToAll($notice->id,1));
            DB::commit();
            admin_success('发布成功', '成功.');            
            
        } catch (\Exception $exception) {
            
            DB::rollBack();
            admin_error('发布失败', '失败.');
        }
    }
    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Notice::findOrFail($id));

        $show->id('Id');
        $show->user_id('User id');
        $show->type_id('Type id');
        $show->title('Title');
        $show->content('Content')->display(function($content){
            $content=htmlspecialchars_decode($content);
            if (mb_strlen($content,'utf-8') > 100){
                $content="<div style='overflow: hidden;text-overflow:ellipsis;white-space: nowrap;'>$content</div>";
            };
            return $content;
        });;
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Notice);

        $admin= Admin::user();

        $form->hidden('user_id')->default($admin['id']);
        // $form->select('type_id', '类型')->options('/admin/noticetype/get_type_list');
        $form->hidden('type_id', '类型')->default(0);
        $form->text('title', '标题');
        $form->text('content', '内容');
        $form->hidden('status', '状态')->default(0);
        
        return $form;
    }
}
